package cn.wuxun.user.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import cn.wuxun.common.pojo.Manager;
import cn.wuxun.common.utils.CookieUtils;
import cn.wuxun.common.vo.EasyUIResult;
import cn.wuxun.common.vo.SysResult;
import cn.wuxun.user.service.ManagerService;
@RestController
public class ManagerController {
	@Autowired
	private ManagerService managerService;
	/**
	 * @return 管理员后台登录
	 */
	@RequestMapping("/managerLogin")
	public SysResult managerLogin(HttpServletRequest request, HttpServletResponse response,Manager manager) {
		// 调用业务层确定合法并且存储数据
				String ticket = managerService.doLogin(manager);
				// 控制层将业务层存储登录成功的rediskey值
				// !"".equals(ticket)&&ticket!=null
				if (StringUtils.isNotEmpty(ticket)) {//
					// ticket非空,redis存好登录的查询结果
					// 将ticket作为cookie的值返回 cookie
					// 的名称,调用Cookie工具类 //添加到cookie返回给前端
					CookieUtils.setCookie(request, response, "ADMIN_TICKET", ticket);
					return SysResult.ok();
				} else {
					// 什么都不做直接返回失败
					return SysResult.build(201, "SHIBAI", null);
				}

	}
	@RequestMapping("/queryManager/{ticket}")
	public SysResult checkLoginManager(@PathVariable String ticket) {
		String managerJson = managerService.queryManagerJson(ticket);
		// 判断非空
		if (StringUtils.isNotEmpty(managerJson)) {
			// 确实曾经登录过.也正在登录使用状态中
			return SysResult.build(200, "ok", managerJson);
		} else {
			return SysResult.build(201, "", null);
		}
	}
	/**
	 * @return 管理员后台注销
	 */
	@RequestMapping("managerLogout")
	public void managerLogout(Manager manager) {
		managerService.logout(manager);
	}


	/**
	 * @return 管理员修改密码
	 */
	@RequestMapping
	public void modifyManagerPassword(Manager manager) {
		managerService.modifyManagerPassword(manager);
	}
	/**
	 * 管理员的增删改查
	 * 查：分页查询
	 * 改，三个都改
	 */
	@RequestMapping("/addManager")
	public SysResult insertManager(Manager manager){
		if(managerService.queryManagerByName(manager)==null){
			return new SysResult(201, "管理员已存在", null);
		}
		try {
			managerService.insertManager(manager);
			return SysResult.ok();
		}catch (Exception e) {
			return new SysResult(201, "插入失败", null);
		}
		
	}
	@RequestMapping("/delManager")
	public SysResult delManager(Manager manager){
		try {
			managerService.delManager(manager);
			return SysResult.ok();
		} catch (Exception e) {
			return new SysResult(201,"删除失败",null);
		}
	}
	//根据mid修改基础信息
	@RequestMapping("/modifyBasicInfo")
	public SysResult modifyManagerBasicInfo(Manager manager){
		try{
			managerService.updateManager(manager);
			return SysResult.ok();
		}catch (Exception e) {
			// TODO: handle exception
			return new SysResult(201,"修改失败",null);
		}
	}
	@RequestMapping("/queryByPage")
	public EasyUIResult queryAllManagerByPage(Integer page,Integer rows){
		EasyUIResult result = managerService.queryAllManagerByPage(page,rows);
		return result;
	}
}
